package it.azienda.moretti.utilita;

import it.azienda.moretti.modello.BeanConfigurazione;
import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailSender {

    private static String email = "";
    private static String pwd = "";

    private static class SMTPAuthenticator extends javax.mail.Authenticator {

        public PasswordAuthentication getPasswordAuthentication() {

            String username = email;

            String password = pwd;

            return new PasswordAuthentication(username, password);

        }
    }

    public static boolean sendMailSMTP(BeanConfigurazione configMailDto,
            String[] to, String title, String msg, File fileAllegato, String nomeAllegato) throws MessagingException {

        Properties props = System.getProperties();

        props.put("mail.smtp.starttls.enable", "true"); // added this line

        props.put("mail.smtp.host", configMailDto.getSmtp());

        props.put("mail.smtp.user", configMailDto.getNomeutente());

        props.put("mail.smtp.password", configMailDto.getPassword());

        props.put("mail.smtp.port", configMailDto.getPorta());

        props.put("mail.smtp.auth", "true");

        //Session session = Session.getDefaultInstance(props, null);
        email = configMailDto.getNomeutente();

        pwd = configMailDto.getPassword();

        Authenticator auth = new SMTPAuthenticator();

        Session session = Session.getDefaultInstance(props, auth);

        Transport transport = session.getTransport("smtp");

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(configMailDto.getNomeutente()));

        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for (int i = 0; i < to.length; i++) { // changed from a while loop

            toAddress[i] = new InternetAddress(to[i]);

        }

        for (int i = 0; i < toAddress.length; i++) { // changed from a while loop

            message.addRecipient(Message.RecipientType.TO, toAddress[i]);

        }

        message.setSubject(title);

        String html = "<html><body>";
        html += msg;
        html += "</body></html>";

        MimeBodyPart mbpf = new MimeBodyPart();
        mbpf.setText(msg);
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mbpf);

        if (fileAllegato != null) {
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(fileAllegato);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(nomeAllegato);
            multipart.addBodyPart(messageBodyPart);
        }

        message.setContent(multipart);

        //message.setText(html);
        // message.setContent(html, "text/html");
        transport.connect();

        transport.sendMessage(message, message.getAllRecipients());

        transport.close();

        return true;

    }
}
